#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "addform.h"
#include "editform.h"

#include <QDebug>
#include <QSqlTableModel>
#include <QSqlQueryModel>
#include <QSqlQuery>
#include <QMessageBox>
#include <QInputDialog>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    queryModel = new QSqlQueryModel(this);
}

MainWindow::~MainWindow()
{
    delete queryModel;
    delete ui;
}

void MainWindow::on_addButton_clicked()
{
    AddForm *form = new AddForm(this);
    form->move(this->x(),this->y());
    form->show();
    this->hide();
    qDebug() << "add";
}


void MainWindow::on_editButton_clicked()
{
    EditForm *edit = new EditForm(this);
    edit->show();
    qDebug() << "edit";
}

void MainWindow::on_queryButton_clicked()
{
    QString search = QInputDialog::getText(this,"搜索","搜索关键字：");

    // query
    if(search == "")
        queryModel->setQuery("SELECT * FROM projects");
    else
    {
        QString searchCondition = "%" + search + "%";

        QString queryStr = QString(R"(
            SELECT *
            FROM projects
            WHERE projectName LIKE '%1'
               OR priority LIKE '%2'
               OR status LIKE '%3'
               OR businessLeader LIKE '%4'
               OR softWareLeader LIKE '%5'
               OR consumeTime LIKE '%6'
               OR requirementsDocumentation LIKE '%7'
               OR bom LIKE '%8'
               OR softwareRecords LIKE '%9'
        )")
                .arg(searchCondition)
                .arg(searchCondition)
                .arg(searchCondition)
                .arg(searchCondition)
                .arg(searchCondition)
                .arg(searchCondition)
                .arg(searchCondition)
                .arg(searchCondition)
                .arg(searchCondition);

        queryModel->setQuery(queryStr);
        ui->tableView->setModel(queryModel);
    }

    // 设置列标题映射
    queryModel->setHeaderData(0, Qt::Horizontal, QObject::tr("项目ID"));
    queryModel->setHeaderData(1, Qt::Horizontal, QObject::tr("项目名称"));
    queryModel->setHeaderData(2, Qt::Horizontal, QObject::tr("优先级"));
    queryModel->setHeaderData(3, Qt::Horizontal, QObject::tr("当前状态"));
    queryModel->setHeaderData(4, Qt::Horizontal, QObject::tr("业务负责人"));
    queryModel->setHeaderData(5, Qt::Horizontal, QObject::tr("软件负责人"));
    queryModel->setHeaderData(6, Qt::Horizontal, QObject::tr("开始时间"));
    queryModel->setHeaderData(7, Qt::Horizontal, QObject::tr("所需时间"));
    queryModel->setHeaderData(8, Qt::Horizontal, QObject::tr("结束时间"));
    queryModel->setHeaderData(9, Qt::Horizontal, QObject::tr("需求文档"));
    queryModel->setHeaderData(10, Qt::Horizontal, QObject::tr("组装BOM"));
    queryModel->setHeaderData(11, Qt::Horizontal, QObject::tr("软件记录"));

    ui->tableView->setModel(queryModel);
    ui->tableView->verticalHeader()->hide();
    qDebug() << "query";
}


void MainWindow::on_deleteButton_clicked()
{
    bool ok;
    int id = QInputDialog::getInt(this, "删除项目", "请输入项目ID：", 0, 1, 9999, 1, &ok);
    if (ok) {
        QSqlQuery q;
        q.prepare("DELETE FROM projects WHERE id = ?");
        q.addBindValue(id);
        if (q.exec() && q.numRowsAffected()) {
            QMessageBox::information(this, "成功", "删除成功");
        } else {
            QMessageBox::critical(this, "错误", "删除失败");
        }
    }
    showData();
}

void MainWindow::showData()
{
    queryModel->setQuery("SELECT * FROM projects");
    // 设置列标题映射
    queryModel->setHeaderData(0, Qt::Horizontal, QObject::tr("项目ID"));
    queryModel->setHeaderData(1, Qt::Horizontal, QObject::tr("项目名称"));
    queryModel->setHeaderData(2, Qt::Horizontal, QObject::tr("优先级"));
    queryModel->setHeaderData(3, Qt::Horizontal, QObject::tr("当前状态"));
    queryModel->setHeaderData(4, Qt::Horizontal, QObject::tr("业务负责人"));
    queryModel->setHeaderData(5, Qt::Horizontal, QObject::tr("软件负责人"));
    queryModel->setHeaderData(6, Qt::Horizontal, QObject::tr("开始时间"));
    queryModel->setHeaderData(7, Qt::Horizontal, QObject::tr("所需时间"));
    queryModel->setHeaderData(8, Qt::Horizontal, QObject::tr("结束时间"));
    queryModel->setHeaderData(9, Qt::Horizontal, QObject::tr("需求文档"));
    queryModel->setHeaderData(10, Qt::Horizontal, QObject::tr("组装BOM"));
    queryModel->setHeaderData(11, Qt::Horizontal, QObject::tr("软件记录"));

    ui->tableView->setModel(queryModel);
    ui->tableView->verticalHeader()->hide();
}

void MainWindow::showEvent(QShowEvent *event)
{
    showData(); // 每次窗口显示时刷新数据
    QMainWindow::showEvent(event);
}



